package httpTool

import (
	"errors"
	"github.com/gin-gonic/gin"
	"strconv"
)

type RegisterRouter interface {
	Register()
}

type BaseController struct {
	RegisterRouter
}

// GetPage 获取分页数据
func (baseController *BaseController) GetPage(ctx *gin.Context) (pageSize int, pageNum int) {

	var err error

	//获取页面大小
	pageSize, err = strconv.Atoi(ctx.Param("pageSize"))
	if err != nil {
		pageSize = 10
	}

	//获取当前多少页
	pageNum, err = strconv.Atoi(ctx.Param("pageNum"))
	if err != nil {
		pageNum = 1
	}

	return pageSize, pageNum
}

// ShouldBandingJson 数据绑定
func (baseController *BaseController) ShouldBandingJson(ctx *gin.Context, target any) {
	if err := ctx.ShouldBindJSON(target); err != nil {
		panic(errors.New("请求数据格式不正确！" + err.Error()))
	}
}

// GetLoginUser 获取登录的用户
func (baseController *BaseController) GetLoginUser(ctx *gin.Context) {

}

// GetLoginUserId 获取登录的用户Id
func (baseController *BaseController) GetLoginUserId(ctx *gin.Context) {

}

func (baseController *BaseController) SuccessData(data any) Result {
	return baseController.Common(200, "操作成功", data)
}

func (baseController *BaseController) SuccessMsg(msg string) Result {
	return baseController.Common(200, msg, nil)
}

func (baseController *BaseController) FailMsg(msg string) Result {
	return baseController.Common(500, msg, nil)
}

func (baseController *BaseController) Common(code int, msg string, data any) Result {
	return Result{code, msg, data}
}
